package cn.xmkeshe.controller;

import cn.xmkeshe.back.ICarsServiceBack;
import cn.xmkeshe.model.Cars;
import cn.xmkeshe.model.Item;
import cn.xmkeshe.utils.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class CarsController {

  @Resource
  private ICarsServiceBack iCarsServiceBack;

//  跳转到增加页面
  @RequestMapping("/pages/back/cars/addPre")
  @Log(operationName = "跳转到车辆增加页面",operationType = "管理员")
  public String addPre(){
    return "/pages/back/cars/add";
  }

  @RequestMapping("/pages/back/cars/add")
  @ResponseBody
  @Log(operationName = "车辆增加",operationType = "系统")
  public String add(Cars cars){
    String flag = "error";
    cars.setCarid(UUID.randomUUID().toString().substring(5,9));
    if(iCarsServiceBack.add(cars)){
      return "success";
    }
    return flag;
  }

//  分页查询车辆
  @RequestMapping("/pages/back/cars/listSplitPre")
  @Log(operationName = "分页查询车辆页面",operationType = "管理员")
  public String listSplitPre(){
    return "/pages/back/cars/list";
  }

  @RequestMapping("/pages/back/cars/listSplit")
  @ResponseBody
  @Log(operationName = "分页查询车辆数据",operationType = "系统")
  public Map<String, Object> listSplit(Cars cars, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iCarsServiceBack.listSplit(currentPage,lineSize);
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("allCars"));//    解析数据列表
    map.put("count",map.get("allCount"));//    解析数据长度
    return map;
  }

  //模糊查询
  @RequestMapping("/pages/back/Cars/findByNumber")
  @ResponseBody
  @Log(operationName = "模糊查询车辆",operationType = "系统")
  public Map<String, Object> findByNumber(String number, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iCarsServiceBack.findByNumber(currentPage,lineSize,"%" + number + "%");
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    System.out.println(map);
    return map;
  }

  @RequestMapping("/pages/back/Cars/edit")
  @ResponseBody
  @Log(operationName = "修改车辆信息",operationType = "管理员")
  public String edit(String carid){
    int status;
    Cars cars = iCarsServiceBack.findById(carid);
    int status1 = cars.getStatus();
    if (status1 == 1){
      status = 2;
    }else{
      status = 1;
    }
    String flag = "error";
    if(iCarsServiceBack.edit(carid,status)){
      return "success";
    }
    return flag;
  }
}
